﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using IPTV.module;
using System.Text;
using System.Xml;

namespace IPTV.util
{
    public class PrimaryUtil
    {
        private const string COURSE_DETAIL_PAGE_URL = "courseDetail.aspx";
        private const string PRIMARY_PLATE_ICON_URL_PREFIT = "../images-jpg/newVer/zt/"; 

        public PrimaryUtil()
        {

        }



        public static void PlateCourseMapToXML(string path, Dictionary<string, List<CourseDto>> plateCourseMap)
        {

            foreach (KeyValuePair<string, List<CourseDto>> item in plateCourseMap)
            {
                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(dec);
                XmlElement root = doc.CreateElement("listitems");
                doc.AppendChild(root);
                XmlNode rowNode = doc.CreateElement("Row");
                root.AppendChild(rowNode);

                try
                {
                    List<CourseDto> courselist = item.Value;
                    foreach (CourseDto course in courselist)
                    {
                        XmlElement entry = doc.CreateElement("Entry");
                        entry.SetAttribute("url", COURSE_DETAIL_PAGE_URL + "?plateId=" + item.Key+"&courseName="+course.VideoName
                                            + "&courseId=" + course.VideoId + "&teacherId=" + course.TeacherId);
                        entry.SetAttribute("title", CommonUtil.subEllipsisText(course.VideoName, 30));
                        entry.SetAttribute("detail", "授课老师:" + course.TeacherName + "\n" + "课程时长:" + course.VideoLength + "\n" + "课程介绍:" + CommonUtil.replaceLineText(course.Remark));
                        entry.SetAttribute("imgUrl", course.PicTwo);
                        rowNode.AppendChild(entry);
                    }

                    doc.Save(path + "\\" + item.Key + ".xml");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }

        }


        public static void PlateToXML(string path, string fileName, List<PlateDto> plateList)
        {
                XmlDocument doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
                doc.AppendChild(dec);
                XmlElement root = doc.CreateElement("Data");
                doc.AppendChild(root);
                XmlNode rowNode = doc.CreateElement("listitems");
                root.AppendChild(rowNode);

                foreach (PlateDto plate in plateList)
                {
                    XmlElement entry = doc.CreateElement("entry");
                    entry.SetAttribute("url", "primaryTopic.aspx?subjectId=" + plate.Subject + "&selPlate=" + plate.Id + "&subjectName=" + plate.SubjectName);
                    entry.SetAttribute("iconUrl", PRIMARY_PLATE_ICON_URL_PREFIT + plate.Icon);
                    rowNode.AppendChild(entry);
                }
                doc.Save(path + "\\" + fileName + ".xml");
        }
    }
}
